#!/bin/bash

# AI助农平台快速启动脚本
echo "🚀 启动AI助农平台..."

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装，请先安装Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose未安装，请先安装Docker Compose"
    exit 1
fi

# 创建环境变量文件
if [ ! -f .env ]; then
    echo "📝 创建环境变量文件..."
    cat > .env << EOF
# MongoDB配置
MONGODB_URI=mongodb://admin:admin123@localhost:27017/zhunong_ai_platform?authSource=admin

# Redis配置
REDIS_URL=redis://localhost:6379

# JWT配置
JWT_SECRET=your-super-secret-jwt-key-change-in-production

# 服务端口
BACKEND_PORT=3000
FRONTEND_PORT=3001
ADMIN_PORT=3002
AI_SERVICE_PORT=8000

# 环境配置
NODE_ENV=development
ENVIRONMENT=development

# 前端配置
FRONTEND_URL=http://localhost:3001
ADMIN_URL=http://localhost:3002
EOF
    echo "✅ 环境变量文件创建完成"
fi

# 启动Docker服务
echo "🐳 启动Docker服务..."
docker-compose up -d

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 30

# 检查服务状态
echo "🔍 检查服务状态..."
docker-compose ps

# 安装依赖（如果需要）
echo "📦 安装项目依赖..."
npm run install:all

echo ""
echo "🎉 AI助农平台启动完成！"
echo ""
echo "📱 访问地址："
echo "   前端商城: http://localhost:3001"
echo "   管理后台: http://localhost:3002"
echo "   后端API: http://localhost:3000"
echo "   AI服务: http://localhost:8000"
echo "   API文档: http://localhost:3000/docs"
echo "   AI文档: http://localhost:8000/docs"
echo ""
echo "🔧 常用命令："
echo "   查看日志: docker-compose logs -f"
echo "   停止服务: docker-compose down"
echo "   重启服务: docker-compose restart"
echo "   查看状态: docker-compose ps"
echo ""
echo "📚 项目文档："
echo "   开发计划: docs/DEVELOPMENT_PLAN.md"
echo "   API文档: docs/API.md"
echo "   数据库设计: database/schema.js"
echo "" 